﻿using Aspose.Words;
using NET6.WebAPI.Extensions;

namespace NET6.WebAPI.Helpers
{
    public static class AsposeWordHelper
    {
        /// <summary>
        /// 根据模板生成 Word(修改文件后缀即可生成 PDF)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data">模型对象</param>
        /// <param name="templatePath">模板路径</param>
        /// <param name="filePath">生成路径</param>
        public static void GenerateWordByTemplate<T>(T data, string templatePath, string filePath) where T : class
        {
            Document doc = new Document(templatePath);

            var properties = typeof(T).GetProperties();

            // 用数据替换占位符
            foreach (var property in properties)
            {
                doc.Range.Replace($"${property.Name}$", property.GetValue(data).ToStr());
            }

            doc.Save(filePath);
        }
    }
}
